#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>

#define FILE_NAME "log.txt"

//每一个宏，对应的数值，只有一个比特位是1，彼此位置不重叠
//#define ONE (1<<0)
//#define TWO (1<<1)
//#define THREE (1<<2)
//#define FOUR (1<<3)
//
//void show(int flags)
//{
//  if(flags & ONE) printf("one\n");
//  if(flags & TWO) printf("two\n");
//  if(flags & THREE) printf("three\n");
//  if(flags & FOUR) printf("four\n");
//}
int main()
{
  //printf("stdin->fd: %d\n",stdin->_fileno);
  //printf("stdout->fd: %d\n",stdout->_fileno);
  //printf("stderr->fd: %d\n",stderr->_fileno);
  umask(0);
  int fd = open(FILE_NAME, O_WRONLY |  O_CREAT | O_APPEND, 0666);
  if(fd < 0)
  {
    perror("open");
    return 1;
  }
  int cnt = 5;

  char outBuffer[64];
  while(cnt)
  {
    sprintf(outBuffer, "%s:%d\n", "helloworld", cnt--);
    write(fd, outBuffer, strlen(outBuffer));
  }

  close(fd);


//  FILE *fp = fopen(FILE_NAME, "a");
//  //FILE *fp = fopen(FILE_NAME, "r");
  //FILE *fp = fopen(FILE_NAME, "a");
//  show(ONE);
//  show(TWO);
//  show(ONE | TWO);
//  show(ONE | TWO | THREE);
//  show(ONE | TWO | THREE | FOUR);
//  
//  if(NULL == fp)
//  {
//    perror("fopen");
//    return 1;
//  }
//
  // char buffer[64];
  // while(fgets(buffer, sizeof(buffer)-1), fp) !=NULL)
  // {
  //    buffer[strlen(buffer)-1] = 0;
  //    puts(buffer);
  // }

 // int cnt = 5;
 // while(cnt)
 // {
 //   fprintf(fp, "%s:%d\n", "hello world", cnt--);
 // }
 // fclose(fp);
}

